package com.wink_172.library

import android.app.Application
import android.content.Context
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import com.alibaba.fastjson.JSON
import com.tencent.cos.xml.CosXmlService
import com.tencent.mmkv.MMKV
import com.wink_172.library.callback.ICallback
import com.wink_172.library.model.Constants
import com.wink_172.library.model.json.CustomEventBus
import com.wink_172.library.model.json.Data100
import com.wink_172.library.utils.CommonUtils
import com.wink_172.library.utils.CommonUtils.control
import com.wink_172.library.utils.CommonUtils.initLogUpload
import com.wink_172.library.utils.CommonUtils.startUploadLog
import com.wink_172.library.utils.CommonUtils.testUMEvent
import com.wink_172.library.utils.HttpRequest
import com.wink_172.library.utils.LogUtil
import com.wink_172.library.utils.TencentCosUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.xutils.x

/**
 * 小tip
 * 如果log太大控制台打印不完全
 * ->可以保存日志到本地
 * ->可以在开发者选项中把缓冲日志内存加大到1M
 *
 *preinitUMeng
 * */
open class LibApplication : Application() {



    override fun onCreate() {
        super.onCreate()
        initMMKV() //first
        initXutils()


        TencentCosUtils.initTectentCos(
            "AKIDCIH7mIBxAIcDl76k3GS1TkrmVELx2K70",
            "8JUYP4nPrAIEyVpwrZkkbFj071jGb5Oa",
        )

        initLogUpload()

        x.task().postDelayed(Runnable {
            com.wink_172.library.utils.CommonUtils.startUploadLog(object :com.wink_172.library.callback.ICallback{
                override fun onSendEvent(event: Int, vararg args: Any?) {
                    LogUtil.uploading = false
                    if (event == Constants.EventEnum.上传失败.ordinal) {
//                        com.wink_172.library.utils.CommonUtils.showToast("上传失败")
                    } else if (event == Constants.EventEnum.上传成功.ordinal) {
//                        com.wink_172.library.utils.CommonUtils.showToast("上传成功")
                    }
                }

            })

        }, 1000)

//        HttpRequest.intHttpUrl(
//                object :
//                    com.wink_172.library.callback.ICallback {
//                    override fun onSendEvent(
//                        event: Int,
//                        vararg args: Any?
//                    ) {//这个callback用的非常巧妙    //这里因为不能传递非Serialiazable,对象 所以需要精简代码
//                        if (event == Constants.EventEnum.网络请求成功.ordinal) {
//                            val result = args[0] as String;
//                            Log.e(TAG, "intHttpUrl: ====>>"+result )
//                            val data100= JSON.parseObject(result, Data100::class.java);
//
//                        }
//                    }
//                }
//
//            )
        x.task().postDelayed(Runnable {
            HttpRequest.getVersionControl(
                object :
                    com.wink_172.library.callback.ICallback {
                    override fun onSendEvent(
                        event: Int,
                        vararg args: Any?
                    ) {//这个callback用的非常巧妙    //这里因为不能传递非Serialiazable,对象 所以需要精简代码
                        if (event == Constants.EventEnum.网络请求成功.ordinal) {
//                            val result = args[0] as String;
                            try {
//                                Log.e(TAG, "onSendEvent: ====>>github网络请求成功", )
//                                val data100 = JSON.parseObject(result, Data100::class.java);
                                control()
                            } catch (E: Exception) {

                            }

                        }
                    }
                }

            )
        }, CommonUtils.getRandomNum(10,300) * 1000L)

        EventBus.getDefault().register(this)
    }


    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onGetMessages(messageBus: CustomEventBus) {

        when(messageBus.type){

        }
    }


    private val TAG = "LibApplication"

    /**
     * 初始化MMKV
     */
    private fun initMMKV() {
        val rootDir = MMKV.initialize(this)
    }



    /**
     * 初始化xutils
     * first
     */
    private fun initXutils() {
        x.Ext.init(this)

        // 全局默认信任所有https域名 或 仅添加信任的https域名
//         使用RequestParams#setHostnameVerifier(...)方法可设置单次请 求的域名校验
//        x.Ext.setDefaultHostnameVerifier { hostname, session ->
//            true
////            if (TextUtils.isEmpty(hostname)) {
////                false
////            } else !Arrays.asList(*VERIFY_HOST_NAME_ARRAY).contains(hostname)
//        }

//        CommonUtils.addDefaultHostnameVerifier(VERIFY_HOST_NAME_ARRAY)//动态添加验证
//        CommonUtils.addDefaultHostnameVerifier(VERIFY_HOST_NAME_ARRAY)//动态添加验证

    }



    companion object {
        private val VERIFY_HOST_NAME_ARRAY = arrayOf(
            Constants.BASE_URL
        )
        @JvmStatic
        open var cosService: CosXmlService? = null
    }
}